package com.mc.moring.util

import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import com.mc.moring.app.LogUtils
import com.mc.moring.core.CoreConfig
import java.net.Inet4Address
import java.net.InetAddress
import java.net.NetworkInterface
import java.util.Collections


object DeviceUtils {

    fun getDeviceIP(): String? {
        try {
            val interfaces = Collections.list(NetworkInterface.getNetworkInterfaces())
            interfaces.forEach { inter ->
                val addrs: List<InetAddress> = Collections.list(inter.inetAddresses)
                addrs.forEach { addr ->
                    if (!addr.isLoopbackAddress && addr is Inet4Address) {
                        return addr.getHostAddress()
                    }
                }
            }
        } catch (e: Exception) {
            LogUtils.logE("DeviceUtils.getDeviceIP")
        }
        return ""
    }

    fun getAppVersion(): String? {
        return try {
            val packageInfo: PackageInfo? =
                CoreConfig.getAppContext().packageManager?.getPackageInfo(
                    CoreConfig.getAppContext().packageName
                        ?: "", 0
                )
            val versionName = packageInfo?.versionName
            LogUtils.logE("DeviceUtils Version name is :$versionName")
            versionName
        } catch (e: PackageManager.NameNotFoundException) {
            LogUtils.logE("DeviceUtils.getAppVersion")
            null
        }
    }

}